import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;


public class DieMenu extends Menu{
	
	SOCApplet applet;
	ArrayList<Player> players;
	private int die1;
	private int die2;
	private BufferedImage dice1;
	private BufferedImage dice2;
	private BufferedImage dice3;
	private BufferedImage dice4;
	private BufferedImage dice5;
	private BufferedImage dice6;
	private boolean visible;
	
	public DieMenu(ArrayList<Player> players, SOCApplet applet){
		this.players = players;
		this.applet = applet;
		
		try {
			dice1 = ImageIO.read(new File("dice1.gif"));
			dice2 = ImageIO.read(new File("dice2.gif"));
			dice3 = ImageIO.read(new File("dice3.gif"));
			dice4 = ImageIO.read(new File("dice4.gif"));
			dice5 = ImageIO.read(new File("dice5.gif"));
			dice6 = ImageIO.read(new File("dice6.gif"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//roles die, checks for thief activation, adds resources to any players with appropriate towns
	@Override
	public void activate(String buttonName) {
		die1 = (int)(Math.random() * 6) + 1;
		die2 = (int)(Math.random() * 6) + 1;
		int dieRoll = die1 + die2;
		System.out.println(dieRoll);
		if(dieRoll == 7){
			applet.thief.activate();
		} else {
			ArrayList<Integer> resources = new ArrayList<Integer>();
			for(Player p : players){
				ArrayList<Settlement> settlements= p.getSettlements();
				for(Settlement s : settlements){
					ArrayList<Tile> tiles = ((Point)s.getLoc()).getAdjTiles();
					for(Tile t : tiles){
						if(t.getDieNum() == dieRoll){
							if(t.getType() == 1){
								p.addBrick(1);
							} else if(t.getType() == 2){
								p.addGrain(1);
							} else if(t.getType() == 3){
								p.addLumber(1);
							} else if(t.getType() == 4){
								p.addOre(1);
							} else if(t.getType() == 5){
								p.addWool(1);
							}
						}
					}
				}
				ArrayList<City> cities = p.getCities();
				for(City s : cities){
					ArrayList<Tile> tiles = ((Point)s.getLoc()).getAdjTiles();
					for(Tile t : tiles){
						if(t.getDieNum() == dieRoll){
							if(t.getType() == 1){
								p.addBrick(2);
							} else if(t.getType() == 2){
								p.addGrain(2);
							} else if(t.getType() == 3){
								p.addLumber(2);
							} else if(t.getType() == 4){
								p.addOre(2);
							} else if(t.getType() == 5){
								p.addWool(2);
							}
						}
					}
				}
			}
		}
		applet.getTurn().setDiceRolled(true);
		visible = true;
	}
	
	private BufferedImage getImageFromNum(int num){
		if(num == 1){
			return dice1;
		} else if(num == 2){
			return dice2;
		} else if(num == 3){
			return dice3;
		} else if(num == 4){
			return dice4;
		} else if(num == 5){
			return dice5;
		} else {
			return dice6;
		}
	}

	@Override
	public void drawSelf(Graphics g) {
		if(die1 != 0 && die2 != 0 && visible){
			
			g.drawImage(getImageFromNum(die1), 10, 450, null);
			g.drawImage(getImageFromNum(die2), 80, 450, null);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			visible = false;
		}
	}

}
